<template>
  <div class="system-user-dialog-container">
    <el-dialog v-model="state.dialog.isShowDialog" :title="state.dialog.title" width="900px">
      <el-form ref="userDialogFormRef" :model="state.ruleForm" :rules="rules" label-width="90px" size="default">
        <el-row :gutter="35">
          <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24" class="mb20">
            <el-form-item label="标题" prop="title">
              <el-input v-model="state.ruleForm.title" :disabled="formReadonly" clearable
                        placeholder="请输入标题"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24" class="mb20">
            <el-form-item label="类型" prop="type">
              <el-select v-model="state.ruleForm.type" :disabled="formReadonly" class="w100" clearable
                         placeholder="请选择">
                <el-option
                    v-for="item in state.noticeList"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24" class="mb20">
            <el-form-item label="公告状态" prop="status">
              <el-radio-group v-model="state.ruleForm.status" class="ml-4">
                <el-radio
                    v-for="dict in state.statusList"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24" class="mb20">
            <el-form-item label="公告内容" prop="content">
              <el-input v-model="state.ruleForm.content" :autosize="{ minRows: 5, maxRows: 30 }"
                        :disabled="formReadonly"
                        placeholder="请输入公告内容"
                        type="textarea"/>
            </el-form-item>

          </el-col>
        </el-row>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button size="default" @click="onCancel">取 消</el-button>
					<el-button size="default" type="primary" @click="onSubmit(userDialogFormRef)">
						{{ state.dialog.submitTxt }}
					</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="sysNoticeDialog" setup>
import {computed, nextTick, onMounted, reactive, ref} from 'vue';
import type {FormInstance, FormRules} from 'element-plus';
import {ElMessage} from 'element-plus';
import {dictDataApi} from "/@/api/sys/dictData";
import {sysNoticeApi} from "/@/api/sys/notice";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
  ruleForm: {
    id: '',
    title: '', // 标题
    type: '', // 类型
    content: '', // 公告内容
    status: '1', // 公告状态
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
  noticeList: [],
  statusList: [],
});


const rules = reactive<FormRules>({
  title: [{required: true, message: '不能为空', trigger: 'blur'}],
  type: [{required: true, message: '不能为空', trigger: 'blur'}],
  content: [{required: true, message: '不能为空', trigger: 'blur'}],
});
// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
  state.dialog.type = type;
  state.dialog.isShowDialog = true;
  if (type === 'edit') {
    state.dialog.title = '修改用户';
    state.dialog.submitTxt = '修 改';
  } else if (type === 'add') {
    state.dialog.title = '新增用户';
    state.dialog.submitTxt = '新 增';
  }

  nextTick(() => {
    if (type === 'add') {
      state.ruleForm.id = '';
      // 清空表单，此项需加表单验证才能使用
      userDialogFormRef.value.resetFields();
    } else {
      state.ruleForm = Object.assign(state.ruleForm, row);
    }
  });
};

// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};
// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      if (state.dialog.type === 'add') {
        sysNoticeApi()
            .addRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '新增成功');
                closeDialog();
                emit('refresh');
              }
            });
      } else if (state.dialog.type === 'edit') {
        sysNoticeApi()
            .updateRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '修改成功');
                closeDialog();
                emit('refresh');
              }
            });
      }
    }
  });
};

// 页面加载时
onMounted(() => {

  // 公告类型
  dictDataApi()
      .getDictByType('sys_notice_type')
      .then(res => {
        state.noticeList = res.data;
      })

  // 系统状态
  dictDataApi()
      .getDictByType('sys_normal_disable')
      .then(res => {
        state.statusList = res.data;
      })

});

const formReadonly = computed(() => {
  return state.dialog.type !== 'add' && state.dialog.type !== 'edit'
})
// 暴露变量
defineExpose({
  openDialog,
});
</script>
